package ro.msg.office.entities;

import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

@Entity
@Table(name="TB_DEPARTMENT")

/******************************************
   Query for:
   *findAll
   *findByName
   *findById
 ******************************************/
@NamedQueries({
	@NamedQuery(name="Department.findAll", query="SELECT d FROM Department d"),
	@NamedQuery(name="Department.findByName", query="SELECT d FROM Department d WHERE d.name = :name"),
	@NamedQuery(name="Department.findById", query="SELECT d FROM Department d WHERE d.departmentId = :id")
})
public class Department{

	@SequenceGenerator(name="DEPARTMENT_SEQUENCE_GENERATOR", 
		    sequenceName="DEPARTMENT_SEQUENCE", 
		            initialValue=1, allocationSize=10)
    /******************************************
	   Generate autoincrement for Id
	 ******************************************/	
	@Id
	@GeneratedValue(strategy=GenerationType.SEQUENCE,
		    generator="DEPARTMENT_SEQUENCE_GENERATOR")
 	@Column(name="departmentId")
  	private Long departmentId;
	
	@Column(name="name")
	private String name;
	
	/******************************************
	   OneToMany relationship 
	 ******************************************/
	@OneToMany(cascade=CascadeType.ALL, mappedBy="department")
	private Set<User> users;
  	
	public Department(){
		super();
	}
	
	public Department(Long departmentId, String name){
		this.departmentId = departmentId;
		this.name = name;
	}

	public Long getDepartmentId() {
		return departmentId;
	}

	public void setDepartmentId(Long departmentId) {
		this.departmentId = departmentId;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Set<User> getUsers() {
		return users;
	}

	public void setUsers(Set<User> users) {
		this.users = users;
	}
	
	public void addUser(User user){
		this.getUsers().add(user);
		user.setDepartment(this);
	}

	

}
